﻿using SAIM.Models;
using SAIM.Models.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SAIM.Controllers
{
    public class NotificationsController : Controller
    {
        private SAIMIdentity UserIdentity
        {
            get { return new SAIMIdentity(User.Identity); }
        }

        // GET: Notifications/RedirectUrl
        public ActionResult RedirectUrl(string id)
        {
            using (NotificationsBLL notification = new NotificationsBLL())
            {
                notification.ReadUserNotification(UserIdentity.GetUserId(), id);
                var url = notification.GetUserNotification(UserIdentity.GetUserId(), id).Url;
                return Redirect(url);
            }
        }

        // GET: Notifications/ReadAll
        [HttpPost]
        public ActionResult ReadAll()
        {
            using (NotificationsBLL notification = new NotificationsBLL())
            {
                if(notification.ReadAll(UserIdentity.GetUserId()))
                    return new JsonResult() { Data = "Success" };
                else
                    return new JsonResult() { Data = "Error" };
            }
        }

        // GET: Notifications/Remove
        public ActionResult Remove(string id)
        {
            using (NotificationsBLL notification = new NotificationsBLL())
            {
                if(notification.RemoveUserNotification(UserIdentity.GetUserId(), id))
                    return new JsonResult() { Data = "Success" };
                else
                    return new JsonResult() { Data = "Error" };
            }
        }
    }
}